package cn.xuewenbao.edu.service.impl;

import cn.xuewenbao.edu.entity.ChildSubject;
import cn.xuewenbao.edu.entity.vo.ChildSubjectVo;
import cn.xuewenbao.edu.mapper.ChildSubjectMapper;
import cn.xuewenbao.edu.service.ChildSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mafayu
 * @since 2021-07-08
 */
@Service
public class ChildSubjectServiceImpl extends ServiceImpl<ChildSubjectMapper, ChildSubject> implements ChildSubjectService {

    //获取一级分类下的所有子分类
    @Override
    public Result<List<ChildSubjectVo>> getAllChildSubject(String parentId) {
        Result<List<ChildSubjectVo>> result = new Result<>();

        List<ChildSubjectVo> subjectVoList = new ArrayList<>();
        //获取二级分类
        QueryWrapper<ChildSubject> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("parent_id", parentId);
        List<ChildSubject> childSubjects = baseMapper.selectList(wrapper1);

        for (ChildSubject childSubject : childSubjects) {

            ChildSubjectVo childSubjectVo = new ChildSubjectVo();
            BeanUtils.copyProperties(childSubject, childSubjectVo);

            //获取三级分类
            QueryWrapper<ChildSubject> wrapper2 = new QueryWrapper<>();
            wrapper2.eq("parent_id", childSubject.getId());
            List<ChildSubject> thirdchildSubjects = baseMapper.selectList(wrapper2);

            List<ChildSubjectVo> thirdVos = new ArrayList<>();
            for (ChildSubject thirdchildSubject : thirdchildSubjects) {
                ChildSubjectVo subjectVo = new ChildSubjectVo();
                BeanUtils.copyProperties(thirdchildSubject, subjectVo);
                thirdVos.add(subjectVo);
            }
            childSubjectVo.setSubjectVoList(thirdVos);
            subjectVoList.add(childSubjectVo);
        }

        result.setResult(subjectVoList);
        return result;
    }
}
